In [2]:
import pandas as pd
In [3]:
!pip install dateutils
In [4]:
earthquakes_df = pd.read_csv("1.0_month.csv")
In [5]:
earthquakes = earthquakes_df.to_dict('records')
In [6]:
earthquakes[1]
Out[6]:
depth_to_words will describe the earthquake's depthmagnitude_to_words will describe the earthquake's power day_in_words should be the day of the weektime_in_words should be "morning", "afternoon", "evening" or "night" date_in_words should be "Monthname day", e.g. "June 22"Any other functions as necessary
In [7]:
earthquake = {
'rms': '1.85',
'updated': '2014-06-11T05:22:21.596Z',
'type': 'earthquake',
'magType': 'mwp',
'longitude': '-136.6561',
'gap': '48',
'depth': '10',
'dmin': '0.811',
'mag': '5.7',
'time': '2014-06-04T11:58:58.200Z',
'latitude': '59.0001',
'place': '73km WSW of Haines, Alaska',
'net': 'us',
'nst': '',
'id': 'usc000rauc'}
In [38]:
def depth_to_words(str_depth):
#'depth': '10'
depth=int(str_depth)
if depth < 70:#not depth > 0
return "shallow"
elif depth < 300:
return "intermediate"
else:#not depth >300
return "deep"
print(depth_to_words(40))
print(depth_to_words(255))
print(depth_to_words(600))
print(depth_to_words(1000))
print(depth_to_words(-1))
In [ ]:
#rewrite the function to accept a dictionary instead of a number
In [39]:
def depth_to_words(earthquake):
depth = int(earthquake['depth'])
if depth < 70:
return "shallow"
elif depth < 300:
return "intermediate"
else:
return "shallow"
# You would call it like this
depth_to_words(earthquake)
Out[39]:
In [35]:
def magnitude_to_words(earthquake):
mag=float(earthquake['mag'])# not round number, use float()
if mag <3:# when do scale, only use one side, no need for >=
return "minor"
elif mag <5:
return "intermediate"
elif mag <7:
return "high"
elif mag <9:
return "outstanding"
else:
return "super"
#print(magnitude_to_words(4))
#print(magnitude_to_words(1))
#print(magnitude_to_words(0.5))
#print(magnitude_to_words(9.5))
#print(magnitude_to_words("11"))
print(magnitude_to_words(earthquake))
In [10]:
#timestring = '2014-06-04T11:58:58.200Z'
#yourdate = dateutil.parser.parse(timestring)
import dateutil.parser
#when dealing with time in python, use strftime
def day_in_words(earthquake):
datestring=earthquake['time']
eq_date=dateutil.parser.parse(datestring)
return eq_date.strftime('%A')
print(day_in_words(earthquake))
In [11]:
#morning:before noon
#afternoon: before 6
#evening:before 9
#.hour is going to give us the hour of the day from the object
def time_in_words(earthquake):
datestring=earthquake['time']
eq_date=dateutil.parser.parse(datestring) #object
if eq_date.hour <3:
return "early morning"
elif eq_date.hour < 12:
return "morning"
elif eq_date.hour < 18:
return "afternoon"
else:
return "night"
print(time_in_words(earthquake))
In [12]:
def date_in_words(earthquake):
datestring=earthquake['time']
eq_date=dateutil.parser.parse(datestring)
return (eq_date.strftime('%B %-d').replace(" 0", " "))
print(date_in_words(earthquake))
PART TWO: Write the eq_to_sentence function
Write a function called eq_to_sentence that, when called, returns the whole sentence mentioned above, "A DEPTH DESCRIPTION, MAGNITUDE earthquake was reported DAY TIME_OF_DAY on DATE LOCATION."
Print out the result for the sample earthquake.
In [40]:
#depth_to_words will describe the earthquake's depth
#magnitude_to_words will describe the earthquake's power
#day_in_words should be the day of the week
#time_in_words should be "morning", "afternoon", "evening" or "night"
#date_in_words should be "Monthname day", e.g. "June 22"
#Any other functions as necessary
depth=depth_to_words(earthquake)
power=magnitude_to_words(earthquake)
day=day_in_words(earthquake)
time_of_the_day=time_in_words(earthquake)
date=date_in_words(earthquake)
"A {} {} {} earthquake was reported {} {} on {}, {}".format(depth, power,earthquake['mag'], day, time_of_the_day, date,earthquake['place'])
Out[40]:
In [17]:
type(earthquake['mag'])
Out[17]:
In [ ]:
#when you have a dictionary, and you do format, use(** format)
In [64]:
name = "Smushface"
animal = "cat"
"My name is {} and I am a {}".format(name, animal)
Out[64]:
In [15]:
animal_dictionary = {
'name': 'Smushface',
'animal': 'cat'
}
"I am a {animal} and my name is {name}".format(**animal_dictionary)
Out[15]:
In [47]:
def eq_to_sentence(earthquake):
eq_to_sentence = {
'depth':depth_to_words(earthquake),
'power':magnitude_to_words(earthquake),
'day':day_in_words(earthquake),
'time_of_the_day':time_in_words(earthquake),
'date':date_in_words(earthquake),
'location':earthquake['place'],
'magnitude': earthquake['mag']
}
return "A {depth} {power} {magnitude} earthquake was reported {day} {time_of_the_day} on {location}, {magnitude}".format(**eq_to_sentence)
eq_to_sentence(earthquake)
Out[47]:
In [72]:
earthquakes_df = pd.read_csv("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_month.csv")
earthquakes_df.head()
Out[72]:
In [ ]:
earthquakes = earthquakes_df.to_dict('records')
PART THREE: Doing it in bulk Read in the csv of the past 30 days of 1.0+ earthquke activity from http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_month.csv (tip: read_csv works with URLs!) Because we haven't covered looping through pandas, use the following code to convert a pandas DataFrame into a list of dictionaries that you can loop through. earthquakes_df = pd.read_csv("1.0_month.csv") earthquakes = earthquakes_df.to_dict('records') (If you really want to do it with pandas, it's for index, row in earthquakes_df.iterrows():) Loop through each earthquake, printing sentence descriptions for the ones that are above or equal to 4.0 on the Richter scale.
In [48]:
#convert the dataframe into a list of dictionaries and pass them to our function, slice off the first 100
for earthquake in earthquakes[0:100]:
print(eq_to_sentence(earthquake))
PART FOUR: The other bits If the earthquake is anything other than an earthquake (e.g. explosion or quarry blast), print There was also a magnitude MAGNITUDE TYPE_OF_EVENT on DATE LOCATION. For example, There was also a magnitude 1.29 quarry blast on June 19 12km SE of Tehachapi, California. with TYPE_OF_EVENT being explosion, quarry blast, etc and LOCATION being 'place' - e.g. '0km N of The Geysers, California'.
In [49]:
earthquakes = earthquakes_df.to_dict('records')
for earthquake in earthquakes:
if earthquake['type'] != 'earthquake':
print("There was also a magnitude", earthquake['mag'], earthquake['type'], "at", earthquake['place'])
else:
pass
In [ ]: